/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI.MenuMain;

import edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.error.ErrorInfo;

/**
 *
 * @author MARS_NHO
 */
public class SplashScreen extends JWindow{
    private int percent = 0; // indicates the status of progess bar
    private Timer t;
    private JProgressBar progessBar;
    private JLabel splashImage;
    private JLabel lb;

    public void showSplash() {
        JPanel content = (JPanel) getContentPane();

        // Set the window's bounds, centering the window
        int width = 565;
        int height = 370;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);

        // Build the splash screen
        splashImage = new JLabel();
        splashImage.setIcon(new ImageIcon(getClass().getResource("/images/SPLASH_HOTEL.png")));
        lb = new JLabel();
        lb.setForeground(new Color(120, 113, 113));
        lb.setHorizontalAlignment(SwingConstants.CENTER);
        lb.setText("Đang kiểm tra kết nối      ");
        progessBar = new javax.swing.JProgressBar();
        // create an action as param for Timer object
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                percent += (Math.random() * 10);
                if (percent > 10) {
                    lb.setText("Đang kiểm tra kết nối.     ");
                }
                if (percent > 20) {
                    lb.setText("Đang kiểm tra kết nối..    ");
                }
                if (percent > 30) {
                    lb.setText("Đang kiểm tra kết nối...   ");
                }
                if (percent > 50) {
                    // keep showing Server Config dialog if connection to database fail
                    while (!BusinessLogicAbstract.checkConnection()) {
                        JXErrorPane jxErr = new JXErrorPane();
                        jxErr.setErrorInfo(new ErrorInfo("Thông Tin Lỗi", "Kết nối thất bại", "<html>Không thể kết nối tới Server.<br/>Vui lòng chỉnh sửa kết nối.</html>", null, null, Level.OFF, null));
                        JXErrorPane.showDialog(null, jxErr);
                        //null, true, "Error!", "<html>Cannot connect to server.<br/>Please config.</html>", CustomMessageDialog.MESSAGE);
                        new jfrm_ChinhSuaServer(null, true);
                    }
                    lb.setText("Kết nối thành công!");
                }
                if (percent >= 100) {
                    t.stop();
                    dispose();
                    // create hibernate config file base connection
                    //new HibernateConfigGenerator(MyConnection.server);
                    new Login().setVisible(true);//Dialog(null, true);
                    
                    
                }
                progessBar.setValue(percent);

            }
        };
        // call action every 200 ms
        t = new Timer(200, action);
        progessBar.setValue(0);
        // timer starts
        t.start();
        content.add(splashImage, BorderLayout.CENTER);
        // create a panel to contain the label and the progess bar
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);
        panel.add(lb, BorderLayout.PAGE_START);
        panel.add(progessBar, BorderLayout.PAGE_END);

        content.add(panel, BorderLayout.SOUTH);

        // Display it
        setVisible(true);
    }
}
